// ve = vue element
let ve_commodity;
$(function () {
   ve_commodity = new Vue({
       el: "#commodity-data-box",
       data: {
           page_index: 1,
           page_num: 1,
           commodities: [],
           producers: [],
           pointed_type: "",
           pointed_producer_id: -1,
           go: 1
       },
       methods: {
           get_paging_data(page) {
               ve_commodity.get_pages_amount();
               ve_commodity.get_producers_data();
               ve_commodity_order.order_button_available = true;

               if (page < 1)
                   page = 1;
               else if (page > ve_commodity.page_num)
                   page = ve_commodity.page_num;

               ajax_get("/commodity/page/" + (page - 1), {
                   type: ve_commodity.pointed_type,
                   producer_id: ve_commodity.pointed_producer_id
               }, function (response) {
                   ve_commodity.commodities = response;
               }, "Getting paging data of commodity")
           },
           get_pages_amount() {
               ajax_get("/commodity/page", {
                   type: ve_commodity.pointed_type,
                   producer_id: ve_commodity.pointed_producer_id
               }, function (response) {
                   ve_commodity.page_num = response;
               }, "Getting pages amount of commodities")
           },
           render_modal_box(id) {
               ve_commodity_info.commodity = ve_commodity.commodities.find(commodity => commodity.id === id)
           },
           render_order_modal_box(id) {
               ve_commodity_order.commodity = ve_commodity.commodities.find(commodity => commodity.id === id);
               ve_commodity_order.message = "";
               ve_commodity_order.credit_number = ve_nav_bar.validation.creditNumber;
               ve_commodity_order.credit_password = ve_nav_bar.validation.creditPassword;
           },
           reset_search() {
               ve_commodity.pointed_type = "";
               ve_commodity.pointed_producer_id = -1;
               ve_commodity.get_paging_data(0)
           },
           get_producers_data() {
               ajax_get("/producer", {}, function (response) {
                   ve_commodity.producers = response;
               }, "Getting all producers")
           }
       }
   });


   let ve_commodity_info = new Vue({
       el: "#commodity-info",
       data: {
           commodity: { producer:{} }
       },
       methods: {
           reset_info() {
               ve_commodity_info.commodity = { producer:{} }
           }
       }
   });

    let ve_commodity_order = new Vue({
        el: "#commodity-order",
        data: {
            image: "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1553017684781&di=d18a057285cbc3e9341621b5d9496864&imgtype=0&src=http%3A%2F%2Fcli.clewm.net%2Ffile%2F2014%2F12%2F10%2F10febbdfabe543c7dd27d74fb4f411f3.png",
            commodity: {},
            amount: 1,
            message: "",
            credit_number: "xxx",
            credit_password: "xxx",
            input_password: ""
        },
        methods: {
            create_order() {

                ajax_post("/order/commodity", {
                    user: {
                        id: ve_nav_bar.validation.id
                    },
                    commodity: {
                        id: ve_commodity_order.commodity.id
                    },
                    amount: 1
                }, function (response) {
                    if (response.code == null) {
                        ve_commodity_order.message = "已创建订单";
                    }
                    else
                        ve_commodity_order.message = response.message;
                    setTimeout(function () {
                        $("#commodity-order").modal('hide');
                    }, 1000);
                }, "Creating pet order")
            }
        },
        computed: {
            is_logged_in() {
                return ve_nav_bar.validation.id > 0;
            }
        }
    })
});